/**
 * 处理页面基本任务
 */
var d1 = require("deviceone");
var dojs = require("dojs");
var do_Global = d1.sm("do_Global");
var do_Page = d1.sm("do_Page");
//数据模型
var provinceListData = d1.mm("do_ListData");
var cityListData = d1.mm("do_ListData");
var districtListData = d1.mm("do_ListData");
//初始化地址信息
var provinceIndex = 0;
var cityIndex = 0;
var districtIndex = 0;
var lastProvinceText = null;
var lastCityText = null;
var lastDistrictText = null;

var lastProvinceID = null;
var lastCityID = null;
var lastDistrictID = null;

var lastProvinceCode = null;
var lastCityCode = null;
var lastDistrictCode = null; 

//控件
var do_Picker_province;
var do_Picker_city;
var do_Picker_district;
var do_ALayout_picker;
//初始化界面
module.exports.initView = function(data, view) {

	do_Picker_province = view.do_Picker_province;
	do_Picker_city = view.do_Picker_city;
	do_Picker_district = view.do_Picker_district;
	do_ALayout_picker = view.do_ALayout_picker;
	if (data.type == 1) {//一级地址
		do_Picker_province.visible = true;
		do_Picker_province.x = 225;
		
		do_Picker_city.visible = false;
		do_Picker_district.visible = false;
		dojs.core.p(data.type);
	} else if (data.type == 2) {//二级地址
		do_Picker_province.visible = true;
		do_Picker_province.x = 0;
		
		do_Picker_city.visible = true;
		do_Picker_city.x = 300;
		
		do_Picker_district.visible = false;

		do_Picker_district.width = 0;
	} else if (data.type == 3) {//三级地址
		do_Picker_province.visible = true;
		do_Picker_province.x = 0;
		do_Picker_province.width = 300;

		do_Picker_city.visible = true;
		do_Picker_city.x = 300;
		do_Picker_city.width = 450;

		do_Picker_district.visible = false;
	}
	do_ALayout_picker.redraw();

}

//初始化地址
module.exports.initAddress = function(data) {

	//显示默认地址
	var defaultAddress = do_Global
			.getMemory(do_Global.getMemory("data_label").location);
	if (defaultAddress.trim().length > 0) {
		var address = defaultAddress.split(" ");
		//查询默认省份位置
		for (var i = 0; i < data.province.length; i++) {
			if (address[0] == data.province[i].name) {
				provinceIndex = i;
				break;
			}
		}
		lastProvinceText = data.province[provinceIndex].name;
		lastProvinceID = data.province[provinceIndex].id;
		lastProvinceCode = data.province[provinceIndex].code;
		//查找市
		var city = address.length == 1 ? address[0] : address[1];
		var cityTemp = data.city[lastProvinceText];
		for (var j = 0; dojs.core.isNullData(cityTemp) && j < cityTemp.length; j++) {
			if (city == cityTemp[j].name) {
				cityIndex = j;
				break;
			}
		}
		lastCityText = cityTemp[cityIndex].name;
		lastCityID = cityTemp[cityIndex].id;
		lastCityCode = cityTemp[cityIndex].code;
		//查找县
	}

	//绑定数据
	provinceListData.addData(getAddressArray(data.province));
	do_Picker_province.bindItems(provinceListData);
	do_Picker_province.index = provinceIndex;

	cityListData.addData(getAddressArray(data.city[lastProvinceText]));
	do_Picker_city.bindItems(cityListData)
	do_Picker_city.index = cityIndex;

	//监听器
	do_Picker_province.on("selectChanged", function(index) {
		lastProvinceText = provinceListData.getOne(index);
		lastProvinceID = data.province[index].id;
		lastProvinceCode = data.province[index].code;
		
		cityListData.removeAll();
		cityListData.addData(getAddressArray(data.city[lastProvinceText]));
		do_Picker_city.index = 0;
		do_Picker_city.refreshItems();
		lastCityText = cityListData.getOne(0);
		lastCityID = data.city[lastProvinceText][0].id;
		lastCityCode = data.city[lastProvinceText][0].code;
	});
	do_Picker_city.on("selectChanged", function(index) {

		lastCityText = cityListData.getOne(index);
		lastCityID = data.city[lastProvinceText][index].id;
		lastCityCode = data.city[lastProvinceText][index].code;
	});
	
	do_Page.fire("finalAddress", {
		province:lastProvinceText,
		provinceID:lastProvinceID,
		provinceCode:lastProvinceCode,
		city:lastCityText == null?"":lastCityText,
		cityID:lastCityID,
		cityCode:lastCityCode
	});
}
//获取最终的地址信息
module.exports.getFinalAddress = function(){
	
	return {
		province:lastProvinceText,
		provinceID:lastProvinceID,
		provinceCode:lastProvinceCode,
		city:lastCityText == null?"":lastCityText,
		cityID:lastCityID,
		cityCode:lastCityCode
	};
}

//---------------------
function getAddressArray(data) {
	
	var array = [];
	for (var i = 0; i < data.length; i++) {
		array.push(data[i].name);
	}
	return array;
}
